package org.jeecg.modules.fw.controller;

import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.business.entity.BusBookingRooms;
import org.jeecg.modules.business.entity.BusRoomsLivingOrder;
import org.jeecg.modules.business.service.IBusBookingRoomsService;
import org.jeecg.modules.business.service.IBusRoomsLivingOrderService;
import org.jeecg.modules.fw.entity.FwLivingJx;
import org.jeecg.modules.fw.service.IFwLivingJxService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.rooms.entity.CesRooms;
import org.jeecg.modules.rooms.service.CesRoomsServiceImpl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: fw_living_jx
 * @Author: jeecg-boot
 * @Date:   2023-06-01
 * @Version: V1.0
 */
@Api(tags="fw_living_jx")
@RestController
@RequestMapping("/fw/fwLivingJx")
@Slf4j
public class FwLivingJxController extends JeecgController<FwLivingJx, IFwLivingJxService> {
	@Autowired
	private IFwLivingJxService fwLivingJxService;
	@Resource
	private IBusRoomsLivingOrderService busRoomsLivingOrderService;
	@Resource
	private IBusBookingRoomsService busBookingRoomsService;
	@Resource
	private CesRoomsServiceImpl cesRoomsService;
	 @Resource
	 private RedisTemplate<String, Object> redisTemplate;

	/**
	 * 分页列表查询
	 *
	 * @param fwLivingJx
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "fw_living_jx-分页列表查询")
	@ApiOperation(value="fw_living_jx-分页列表查询", notes="fw_living_jx-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<FwLivingJx>> queryPageList(FwLivingJx fwLivingJx,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FwLivingJx> queryWrapper = QueryGenerator.initQueryWrapper(fwLivingJx, req.getParameterMap());
		Page<FwLivingJx> page = new Page<FwLivingJx>(pageNo, pageSize);
		IPage<FwLivingJx> pageList = fwLivingJxService.page(page, queryWrapper);
		pageList.getRecords().forEach(t -> {
			BusRoomsLivingOrder busRoomsLivingOrder = busRoomsLivingOrderService.getById(t.getLivingOrderId());
			if (busRoomsLivingOrder != null) {
				BusBookingRooms busBookingRooms = busBookingRoomsService.getById(busRoomsLivingOrder.getBookingRoomId());
				if (busBookingRooms != null) {
					t.setRoomId(busBookingRooms.getRoomId());
					CesRooms cesRooms = cesRoomsService.getById(busBookingRooms.getRoomId());
					if (cesRooms != null) {
						t.setRoomName(cesRooms.getName());
					}
				}
			}
		});
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param fwLivingJx
	 * @return
	 */
	@AutoLog(value = "fw_living_jx-添加")
	@ApiOperation(value="fw_living_jx-添加", notes="fw_living_jx-添加")
	//@RequiresPermissions("fw:fw_living_jx:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody FwLivingJx fwLivingJx) {
		fwLivingJxService.save(fwLivingJx);
		fwLivingJxService.addJxCache(fwLivingJx);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param fwLivingJx
	 * @return
	 */
	@AutoLog(value = "fw_living_jx-编辑")
	@ApiOperation(value="fw_living_jx-编辑", notes="fw_living_jx-编辑")
	//@RequiresPermissions("fw:fw_living_jx:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody FwLivingJx fwLivingJx) {
		boolean b = fwLivingJxService.updateById(fwLivingJx);
		if (b){
			redisTemplate.opsForZSet().remove("jxLiving", fwLivingJx.getId());
			fwLivingJxService.addJxCache(fwLivingJx);
		}
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "fw_living_jx-通过id删除")
	@ApiOperation(value="fw_living_jx-通过id删除", notes="fw_living_jx-通过id删除")
	//@RequiresPermissions("fw:fw_living_jx:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		fwLivingJxService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "fw_living_jx-批量删除")
	@ApiOperation(value="fw_living_jx-批量删除", notes="fw_living_jx-批量删除")
	//@RequiresPermissions("fw:fw_living_jx:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.fwLivingJxService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "fw_living_jx-通过id查询")
	@ApiOperation(value="fw_living_jx-通过id查询", notes="fw_living_jx-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<FwLivingJx> queryById(@RequestParam(name="id",required=true) String id) {
		FwLivingJx fwLivingJx = fwLivingJxService.getById(id);
		if(fwLivingJx==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(fwLivingJx);
	}


	 /**
	  * 通过id查询
	  *
	  * @param hotelId 酒店id
	  * @return List<CesRooms>
	  */
	 //@AutoLog(value = "fw_living_jx-通过id查询")
	 @ApiOperation(value="getJxRooms-查询所有要叫醒的房间", notes="getJxRooms-查询所有要叫醒的房间")
	 @GetMapping(value = "/getJxRooms")
	 public Result<List<CesRooms>> getJxRooms(String hotelId) {
		 Calendar calendar = Calendar.getInstance();
		 calendar.set(Calendar.HOUR_OF_DAY, 0);
		 calendar.set(Calendar.MINUTE, 0);
		 calendar.set(Calendar.SECOND, 0);
		 calendar.set(Calendar.MILLISECOND, 0);
		 List<FwLivingJx> jxList = fwLivingJxService.list(Wrappers.<FwLivingJx>lambdaQuery().ge(FwLivingJx::getJxDate, calendar.getTime()));
		 Date newDate = new Date();
		 List<String> livingIds = new ArrayList<>();
		 jxList.forEach(e -> {
			 Calendar c = getDateTime(e.getJxTime());
			 if (c.getTime().after(newDate)){
				 livingIds.add(e.getLivingOrderId());
			 }
		 });
		 if (livingIds.isEmpty()){
			 return Result.OK(new ArrayList<>());
		 }
		 List<CesRooms> cesRooms = cesRoomsService.list(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getHotelId,hotelId)
				 .in(CesRooms::getLivingOrderId, livingIds));
		 return Result.OK(cesRooms);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param fwLivingJx
    */
    //@RequiresPermissions("fw:fw_living_jx:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FwLivingJx fwLivingJx) {
        return super.exportXls(request, fwLivingJx, FwLivingJx.class, "fw_living_jx");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("fw:fw_living_jx:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FwLivingJx.class);
    }

	public Calendar getDateTime(String timeStr){
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		Date time;
		try {
			time = format.parse(timeStr);
		} catch (ParseException ex) {
			throw new RuntimeException(ex);
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		Calendar lastCalendar = Calendar.getInstance();
		lastCalendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR));
		lastCalendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
		lastCalendar.set(Calendar.SECOND, 0);
		lastCalendar.set(Calendar.MILLISECOND, 0);
		return lastCalendar;
	}

}
